package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import session_beans.AirTransportSessionBean;
import session_beans.AirTransportSessionRemote;

public class AddNewCityServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

	private static final long serialVersionUID = 771210617304792314L;

	private AirTransportSessionRemote airTransportSession;
	
	@Override
	public void init() throws ServletException {
		super.init();
		
		airTransportSession = new AirTransportSessionBean();
	}
	
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String idParam = request.getParameter("cityID");
		String nameParam = request.getParameter("cityName");

		if (idParam != null && nameParam != null && idParam.length() > 0 && nameParam.length() > 0) {
			
			if ( airTransportSession.addNewCity(idParam, nameParam) == true ) {
				response.sendRedirect("viewer/newcity.jsp?cityname=\"" + nameParam + "\"&" + "flag=true");
				return;
			}
			else {
				response.sendRedirect("viewer/newcity.jsp?cityname=\"" + nameParam + "\"&" + "flag=false");
				return;
			}
		}
		else {
			response.sendRedirect("viewer/newcity.jsp");
		}
		
	}

}
